mini.parse();

function PwdQuesHandler() {
};

PwdQuesHandler.instance = new PwdQuesHandler();

//获得全局的密保问题数据表格对象
PwdQuesHandler.pwdQuesGrid = mini.get("pwdQuesGrid");

//加载表格数据
PwdQuesHandler.pwdQuesGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
PwdQuesHandler.loadToolbar = function() {
	var array = new Array("pwd_question_add", "pwd_question_edit", "pwd_question_del", "pwd_question_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
PwdQuesHandler.loadToolbar();

/**
 * 弹出添加密保问题窗口
 */
PwdQuesHandler.addPwdQues = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(341, 170, "添加密保问题"); 
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("pwdQuestionForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.PwdQuesFormHandler.passData(data);
	},function(action){
		PwdQuesHandler.pwdQuesGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑密保问题窗口
 */
PwdQuesHandler.editPwdQues = function() {
	var rowData = PwdQuesHandler.pwdQuesGrid.getSelected();
	var rowsData = PwdQuesHandler.pwdQuesGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(341, 170, "编辑密保问题[<font color='blue'>" + rowData.bpqContent + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("pwdQuestionForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bpqId: rowData.bpqId};
            iframe.contentWindow.PwdQuesFormHandler.passData(data);
		},function(action){
			PwdQuesHandler.pwdQuesGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除密保问题资源
 */
PwdQuesHandler.delPwdQues = function() {
	var rowsData = PwdQuesHandler.pwdQuesGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bpqId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bpqId;
					
					// 判断选择的密保问题是否已被会员所使用
					if(PwdQuesHandler.getBmbBybpq(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在会员信息关联，请检查后重试！");
						return;	
					}
					
					PwdQuesHandler.pwdQuesGrid.loading();
                    $.ajax({
                        url: __root + "/delBpqByIds.do",
                        data: {
				        	bpqIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		PwdQuesHandler.pwdQuesGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	PwdQuesHandler.pwdQuesGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 判断选择的密保问题是否已被会员所使用
 * @param {} bsiIds
 * @return {}
 */
PwdQuesHandler.getBmbBybpq = function(bpqIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBmbBybpq.do?bpqIds=" + bpqIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新密保问题状态
 */
PwdQuesHandler.updatePwdQuesState = function() {
	if (!MiniuiTools.hasPurview("pwd_question_edit")) {
		return;
	}
	
	var rowData = PwdQuesHandler.pwdQuesGrid.getSelected();
	var bpqId = rowData.bpqId;	
	PwdQuesHandler.pwdQuesGrid.loading();
	
    $.ajax({
        url: __root + "/updateBpqStateById.do",
        data: {
        	bpqId: bpqId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            PwdQuesHandler.pwdQuesGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);        	
        },
        complete: function(){
        	PwdQuesHandler.pwdQuesGrid.unmask();
        }
    });	
};

/**
 * 刷新页面数据及布局
 */
PwdQuesHandler.afresh = function(){
	PwdQuesHandler.pwdQuesGrid.reload();
}

/**
 * DrawCell 绘制单元格
 */
PwdQuesHandler.pwdQuesGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bpqState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='PwdQuesHandler.updatePwdQuesState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='PwdQuesHandler.updatePwdQuesState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

